<?php

	class Module_admin_backup
	{
		public static function execute()
		{
			Auth::getInstance()->getPermis('admin/backup');

			$vis = new Visual();

			$backups = array();

			$dh = opendir(BACKUP_PATH);
			while ($file = readdir($dh)) {
				if ($file == '.' || $file == '..') continue;

				$s      = explode('_', $file, 3);
				$prefix = $s[0] . '_' . $s[1];
				$date   = strtotime($s[0] . ' ' . $s[1]);

				$backups[$prefix]['timestamp']  = $date;
				$backups[$prefix]['date']       = date('d.m.Y H:i:s', $date);
				$backups[$prefix]['filename'][] = $file;
			}
			krsort($backups);
			$vis->assign('backups', $backups);

			return $vis->fetch('admin/backup.twig');
		}

		public static function backup()
		{
			Auth::getInstance(true)->getPermis('admin/backup');

			$time = time();
			$date = date('Ymd_His', $time);

			$dh = opendir(CONFIG_PATH . '/Database');
			while ($filename = readdir($dh)) {
				if ($filename == '.' || $filename == '..') continue;

				$file   = basename($filename, '.php');
				$config = 'Config_Database_' . $file;
				exec('mysqldump ' . $config::$db_name . ' --user=' . $config::$db_login . ' --password=' . $config::$db_password . '|gzip -c9 > ' . BACKUP_PATH . '/' . $date . '_db_' . $file . '.sql.gz');

			}

			$exclude = array(
				'*.log', '*.svn*', '*.idea*', 'backup/*', 'prices/*'
			);

			$cmd = 'tar -Pcvzf ' . BACKUP_PATH . '/' . $date . '_www.tar.gz';
			foreach ($exclude as $s) {
				$cmd .= ' --exclude=' . $s;
			}
			$cmd .= ' --directory=' . ROOT_PATH . ' *';

			$result = shell_exec($cmd);

			return array('result' => $result,
						 'row'    => array(
							 'date'   => date('d.m.Y H:i:s', time()),
							 'prefix' => $date,
						 ));
		}

		public static function restore($params)
		{
			Auth::getInstance(true)->getPermis('admin/backup');

			$result = '';
			$dh     = opendir(BACKUP_PATH);


			while ($file = readdir($dh)) {
				if ($file == '.' || $file == '..') continue;

				$s      = explode('_', $file);
				$prefix = $s[0] . '_' . $s[1];

				if ($prefix != $params['prefix']) continue;

				if ($s[2] == 'db') {
					$result .= "DB RESTORE:{$file}\n";

					$db_name = basename($s[3], '.sql.gz');

					$config = 'Config_Database_' . $db_name;

					$db = Database::getInstance($db_name);

					$tables = $db->getTables();
					foreach ($tables as $table) {
						$db->query('DROP TABLE IF EXISTS `' . $table . '`');
					}

					$cmd = 'gunzip < ' . BACKUP_PATH . '/' . $file . ' | mysql --user=' . $config::$db_login . ' --password=' . $config::$db_password . ' ' . $config::$db_name;

					$result .= shell_exec($cmd);

					unset($db);
				} else {
					$result .= "WWW RESTORE:{$file}\n";
					$result .= shell_exec('tar -xvf ' . BACKUP_PATH . '/' . $file . ' --directory=' . ROOT_PATH . ' --overwrite');
				}

			}

			return array('result' => $result);
		}

		public static function delete($params)
		{
			Auth::getInstance(true)->getPermis('admin/backup');

			$dh = opendir(BACKUP_PATH);

			while ($file = readdir($dh)) {
				if ($file == '.' || $file == '..') continue;

				$s      = explode('_', $file);
				$prefix = $s[0] . '_' . $s[1];

				if ($prefix != $params['prefix']) continue;

				unlink(BACKUP_PATH . '/' . $file);
			}

			return array('result' => 'ok');
		}
	}